﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SundayWishModel.Entities;
using NHibernate;

namespace SundayWishBLL
{
    public class UserHelper
    {
        public IList<UserInfo> GetUser()
        {
            ISession Session = NHibernateHelper.GetSession();
            return Session.CreateQuery("from UserInfo").List<UserInfo>();
        }

        public UserInfo GetUser(int UserId)
        {
            ISession Session = NHibernateHelper.GetSession();
            return Session.Get<UserInfo>(UserId);
        }
        public IList<UserInfo> GetUserByUserName(string UserName)
        {
            ISession Session = NHibernateHelper.GetSession();
            return Session.CreateQuery("select u from UserInfo u where UserName=:un")
                .SetString("un",UserName)
                .List<UserInfo>();
        }

        public Boolean AddUser(UserInfo user)
        {
            ISession Session = NHibernateHelper.GetSession();
            if (Session.CreateQuery("select u from UserInfo u where UserName=:un")
                .SetString("un", user.UserName)
                .List<UserInfo>().Count > 0)
                return false;
            Session.Save(user);
            Session.Flush();
            HttpContext.Current.Session["user"] = user;
            return true;
        }

        public Boolean UpdateUser(UserInfo user)
        {
            ISession Session = NHibernateHelper.GetSession();
            Session.Update(user);
            Session.Flush();
            return true;
        }

        public Boolean DeleteUser(UserInfo user)
        {
            ISession Session = NHibernateHelper.GetSession();
            Session.Delete(user);
            Session.Flush();
            return true;
        }

        public Boolean Login(string UserName, string Password)
        {
            ISession Session = NHibernateHelper.GetSession();
            IList<UserInfo> user = Session.CreateQuery("select u from UserInfo u where UserName=:un and UserPassword=:pwd")
                .SetString("un", UserName)
                .SetString("pwd", Password)
                .List<UserInfo>();
            if (user.Count == 0)
                return false;
            else
            {
                user[0].LastLogin = DateTime.Now;
                Session.Update(user[0]);
                Session.Flush();
                HttpContext.Current.Session["user"] = user[0];
                return true;
            }
        }

        public Boolean Logout()
        {
            UserInfo user = (UserInfo)HttpContext.Current.Session["user"];
            user.LastLogout = DateTime.Now;
            ISession Session = NHibernateHelper.GetSession();
            Session.Update(user);
            Session.Flush();
            HttpContext.Current.Session.Clear();
            return true;
        }

        public bool ChangePassword(int UserId, string OldPassword, string NewPassword)
        {
            ISession Session = NHibernateHelper.GetSession();
            UserInfo ui = Session.Get<UserInfo>(UserId);
            if (ui.UserPassword == OldPassword)
            {
                ui.UserPassword = NewPassword;
                Session.Update(ui);
                Session.Flush();
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ChangePhone(int UserId, string OldPhone, string NewPhone)
        {
            ISession Session = NHibernateHelper.GetSession();
            UserInfo ui = Session.Get<UserInfo>(UserId);
            if (ui.PhoneNumber == NewPhone)
            {
                ui.PhoneNumber = OldPhone;
                Session.Update(ui);
                Session.Flush();
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
